/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.IdGen;
import com.kaaction.modules.mer.entity.Attribute;
import com.kaaction.modules.mer.entity.Sku;
import com.kaaction.modules.mer.entity.Spu;
import com.kaaction.modules.sys.entity.User;
import com.kaaction.modules.sys.utils.UserUtils;
import com.kaaction.modules.mer.dao.AttributeValueDao;
import com.kaaction.modules.mer.dao.MerTagDao;
import com.kaaction.modules.mer.dao.SkuDao;
import com.kaaction.modules.mer.dao.SpuDao;

/**
 * 商品分类Service
 * @author huanghaiming
 * @version 2017-05-19
 */
@Service
@Transactional(readOnly = true)
public class SpuService extends CrudService<SpuDao, Spu> {

	@Autowired
	AttributeValueDao attributeValueDao;
	
	@Autowired
	SkuDao skuDao;
	
	@Autowired
	MerTagDao merTagDao;
	
	public Spu get(String id) {
		return super.get(id);
	}
	
	public List<Spu> findList(Spu spu) {
		return super.findList(spu);
	}
	
	public Page<Spu> findPage(Page<Spu> page, Spu spu) {
		return super.findPage(page, spu);
	}
	
	@Transactional(readOnly = false)
	public void save(Spu spu) {
		
		String uuid = IdGen.uuid();
		String id = spu.getId();
		
		spu.setTag(arrayToString(spu.getTag5()));
	/*	spu.setTag1(arrayToString(spu.getTag6()));
		spu.setTag2(arrayToString(spu.getTag7()));
		spu.setTag3(arrayToString(spu.getTag8()));
		spu.setTag4(arrayToString(spu.getTag9()));*/
		
		if (spu.getIsNewRecord()){
			
			User user = UserUtils.getUser();
			
			if (StringUtils.isNotBlank(user.getId())){
				spu.setCreateBy(user);
				spu.setUpdateBy(user);
			}
			spu.setUpdateDate(new Date());
			spu.setCreateDate(new Date());
			
			spu.setId(uuid);
			spu.setIsBanner("0");
			dao.insert(spu);
			
		}else{
			spu.preUpdate();
			dao.update(spu);
		}
		
		List<Attribute> attrList= spu.getAttrList();
		
		for(Attribute attr:attrList){
			
			if (StringUtils.isBlank(id)){
				attr.getAttributeValue().preInsert();
				attr.getAttributeValue().setSpuId(uuid);
				attributeValueDao.insert(attr.getAttributeValue());
			}else{
				attr.getAttributeValue().preUpdate();
				attributeValueDao.update(attr.getAttributeValue());
			}
			
		}
		
		List<Sku> skuList= spu.getEventSkuList();
		
		for(Sku sku:skuList){
			
			if(sku.getColorId()!=null){
				
				int cnt  = skuDao.selectCnt(sku);
				if (cnt==0){
					sku.preInsert();
					if (StringUtils.isBlank(id)){
						sku.setSpuId(uuid);
					}else{
						sku.setSpuId(id);
					}
					sku.setDelFlag("0");
					skuDao.insert(sku);
				}else{
					sku.preUpdate();
					skuDao.update(sku);
				}
			}
		}
		
	}
	
	
	private String arrayToString(String[] str){
		
		String st = "";
		for(String s: str){
			st+=s+",";
		}
		if(StringUtils.isNotBlank(st)){
			st.substring(0, st.length()-1);
		}
		return st;
	}
	
	@Transactional(readOnly = false)
	public void delete(Spu spu) {
		super.delete(spu);
	}

	@Transactional(readOnly = false)
	public void updateUpOrDown(Spu spu) {
		super.dao.updateUpOrDown(spu);
		
	}

	@Transactional(readOnly = false)
	public void updateUpOrDownHome(Spu spu) {
		super.dao.updateUpOrDownHome(spu);
	}
	
	public List<Map<String, Object>> spuClassifyDetail(Map<String, Object> map) {
		return super.dao.spuClassifyDetail(map);
	}

	public Map<String, Object> selectProductDetail(String id) {
		
		Map<String, Object>  map = super.dao.selectProductDetail(id);//商品详情
		
		if(map!=null){
			
			List<Map<String, Object>> listColor = skuDao.selColor(id);
			if(listColor.size()>0){
				map.put("price", listColor.get(0).get("price"));
			}
			map.put("listColor", listColor);//颜色
			map.put("listSize", skuDao.selSize(id));//尺寸
			map.put("listSku", skuDao.selSku(id));//销售属性
			map.put("listAttr", attributeValueDao.selAttr(id));//非关键属性
			map.put("listTag", selTag((String)map.get("tag")));//标签
			/*map.put("listTag1", selTag((String)map.get("tag1")));//标签1
			map.put("listTag2", selTag((String)map.get("tag2")));//标签2
			map.put("listTag3", selTag((String)map.get("tag3")));//标签3
			map.put("listTag4", selTag((String)map.get("tag4")));//标签4
*/		}
		return map;
	}
	
	private List<Map<String,Object>> selTag(String str) {
		
		List<Map<String,Object>> liMap = null;
		
		if(StringUtils.isNotBlank(str)){
			String[] st = str.split(",");
			liMap = merTagDao.selectTag(Arrays.asList(st));
		}else{
			liMap = Lists.newArrayList(); 
		}
		return liMap;
	}

	public List<Map<String, Object>> selecProductClassify(Map<String, Object> map) {
		return super.dao.selecProductClassify(map);
	}

	public List<Map<String, Object>> spuTagShow(Map<String, Object> map) {
		return super.dao.spuTagShow(map);
	}

	public List<Map<String, Object>> selSup(String id) {
		return super.dao.selSup(id);
	}

	public List<Map<String, Object>> selectHome() {
		return super.dao.selectHome();
	}

	public int spuTagShowCount(Map<String, Object> map) {
		return  super.dao.spuTagShowCount(map);
	}


}